package com.dz.xjd.ui.presenter;

import com.dz.xjd.app.App;
import com.dz.xjd.base.baserx.RxSubscriber;
import com.dz.xjd.bean.ProfessionBean;
import com.dz.xjd.bean.ResponseResult;
import com.dz.xjd.ui.contract.ProfessionContract;
import com.dz.xjd.utils.EncryptDecrypt;
import com.dz.xjd.utils.GsonUtil;
import com.dz.xjd.utils.KeyUtil;
import com.dz.xjd.utils.ToastUtil;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

public class ProfessionPresenter extends ProfessionContract.Presenter{
    @Override
    public void getProfessionRequest(String header, String params) {
        mModel.getProfessionData(header, params)
                .subscribe(new RxSubscriber<String>(mContext,true){
                    @Override
                    protected void _onNext(String data) {
                        Logger.d("data---"+data);
                        ResponseResult responseResult = GsonUtil.jsonToBean(data,ResponseResult.class);
                        int code = responseResult.getCode();
                        if(code == 0){
                            String[] keys = KeyUtil.getKey(App.getToken());
                            try {
                                String userInfo = EncryptDecrypt.decrypt(keys[0],responseResult.getData(),keys[1]);
                                Logger.d("getLoginRequest---"+userInfo);
                                ProfessionBean professionBean = GsonUtil.jsonToBean(userInfo,ProfessionBean.class);
                                mView.returnProfessionData(professionBean);
                                Logger.d("userBean----"+new Gson().toJson(professionBean));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            ToastUtil.showShort("登录成功");
                        }else if(code == 2){
                            Logger.d("token 过期---");
                        }else{
                            Logger.d("其他---");
                        }

                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtil.showShort(message);
                    }
                });
    }


}
